using System;
using System.Collections.Specialized;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data.SqlClient;

using Common;
public partial class tm_TreeTab : System.Web.UI.UserControl
{
    private ArrayList selectedNodes;//get value on checkbox from web parent
    private int myHeight = 200;
    private int myWidth = 400;

    //global treeview
    public TreeView myTreeView
    {
        get { return this.TreeViewObj; }
    }
    public ArrayList SelectedNodes
    {
        set { selectedNodes = value; }
        get { return selectedNodes; }
    }


    public int MyHeight
    {
        set { myHeight = value; }
        get { return myHeight; }
    }

    public int MyWidth
    {
        set { myWidth = value; }
        get { return myWidth; }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            myTreeView.Attributes.Add("onclick", "OnTreeClick(event)");

            PopulateRootLevel("02");

            //set Width and Height panel
            paTreeview.Height = MyHeight;
            paTreeview.Width = myWidth;

            //get Session selectedNodes
            if (selectedNodes != null)
                Session["selectedNodes"] = selectedNodes;
            else
                Session.Remove("selectedNodes");

            //check all
            TreeNodeCollection items = myTreeView.Nodes;
            CheckAll(items);


        }
    }

    protected void TreeViewObj_TreeNodePopulate(object sender, TreeNodeEventArgs e)
    {
        PopulateSubLevel(e.Node.Value, e.Node);

        //TreeView treeView = TreeViewObj as TreeView;
        TreeNodeCollection items = myTreeView.Nodes;
        //check all
        CheckAll(items);

    }

    protected void CheckAll(TreeNodeCollection items)
    {
        //if (Session["tm_arrayListTT"] != null)
        if (selectedNodes != null)
        {
            ArrayList glo_selectedNodes = (ArrayList)Session["selectedNodes"];

            if ((myTreeView != null) && (items != null))
            {
                foreach (TreeNode item in items)
                {
                    for (int i = 0; i < glo_selectedNodes.Count; i++)
                    {
                        if (glo_selectedNodes[i].ToString().Trim() == item.Value.ToString().Trim())
                            item.Checked = true;
                        foreach (TreeNode temp in item.ChildNodes)
                        {
                            if (glo_selectedNodes[i].ToString().Trim() == temp.Value.ToString().Trim())
                            {
                                temp.Checked = true;
                            }
                            else CheckAll(temp.ChildNodes);

                        }
                    }
                }
            }
        }
    }
    protected void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
    {
        nodes.Clear();

        foreach (DataRow dr in dt.Rows)
        {
            TreeNode tn = new TreeNode();
            tn.Text = dr["description1"].ToString().Trim();
            tn.Value = dr["spcCode"].ToString().Trim();
            nodes.Add(tn);

            //If node has child nodes, then enable on-demand populating
            tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
        }
    }
    protected void PopulateRootLevel(string parentCode)
    {
        SqlConnection objConn = new SqlConnection(sysLibs.GetConnectionString);
        SqlCommand objCommand = new SqlCommand(@"select description1, spcCode ,(select count(*) FROM tm_spccode WHERE parentCode=sc.spcCode ) childnodecount FROM tm_spccode sc WHERE     (parentCode =N'" + parentCode.Trim() + "') ", objConn);
        SqlDataAdapter da = new SqlDataAdapter(objCommand);
        DataTable dt = new DataTable();
        da.Fill(dt);
        PopulateNodes(dt, myTreeView.Nodes);
    }

    protected void PopulateSubLevel(string ParentCode, TreeNode parentNode)
    {
        SqlConnection objConn = new SqlConnection(sysLibs.GetConnectionString);


        SqlCommand objCommand = new SqlCommand(@"select description1 ,spcCode,(select count(*) FROM tm_spccode WHERE ParentCode=sc.spcCode) childnodecount FROM tm_spccode sc where (parentCode=@ParentCode)", objConn);
        objCommand.Parameters.Add("@ParentCode", SqlDbType.Char, 10).Value = ParentCode;
        SqlDataAdapter da = new SqlDataAdapter(objCommand);
        DataTable dt = new DataTable();
        da.Fill(dt);
        PopulateNodes(dt, parentNode.ChildNodes);
    }
    //end show treeview

    protected void btnChon_Click(object sender, EventArgs e)
    {
        //Hashtable tm_array = new Hashtable();
        //ArrayList tm_arrayList = new ArrayList();

        //if (TreeViewObj.CheckedNodes.Count > 0)
        //{

        //    foreach (TreeNode item in TreeViewObj.CheckedNodes)
        //    {
        //        tm_array.Add(item.Value.ToString(), item.Text.ToString());
        //        tm_arrayList.Add(item.Value.ToString().Trim());
        //    }
        //}


        ////return array Hashtable
        //Session["tm_ReturnArrayTT"] = tm_array;

        ////return array List
        //Session["tm_arrayListTT"] = tm_arrayList;

    }

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        TreeViewObj_Unload(sender, e);

        TreeNodeCollection items = myTreeView.Nodes;
        //check all
        CheckAll(items);
        
    }

    protected void TreeViewObj_Unload(object sender, EventArgs e)
    {
        string subGroup = DropDownList1.SelectedValue.ToString();
        PopulateRootLevel(subGroup.Trim());
    }


    public ArrayList getSelectedNodesArray()
    {
        ArrayList seletedNodes = new ArrayList();

        if (myTreeView.CheckedNodes.Count > 0)
            foreach (TreeNode tn in myTreeView.CheckedNodes)
            {
                seletedNodes.Add(tn.Value.Trim());
            }
        return seletedNodes;
    }

    public Hashtable getSelectedNodesHashtable()
    {
        Hashtable seletedNodes = new Hashtable();

        if (myTreeView.CheckedNodes.Count > 0)
            foreach (TreeNode tn in myTreeView.CheckedNodes)
            {
                seletedNodes.Add(tn.Text.Trim(), tn.Value.Trim());
            }
        return seletedNodes;
    }
}
